package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.SerializableMap;
import com.wwwarehouse.resource_center.bean.rules.RuleDesk;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.Event;
import com.wwwarehouse.resource_center.eventbus_event.HideTitleBean;
import com.wwwarehouse.resource_center.utils.EventBusUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/8/22.
 *         <p/>
 *         编辑任务分配规则列表
 */

public class EditTaskRuleListFragment extends TheParentFragment {

    private ListView lvlist;
    private RuleListAdaper mAdapter;
    private static final int SEARCH_TASK_ALLOCATION_RULE_DESKTOP = 1;
    private int pos;
    private int num;
    private boolean isFunction ;
    private List<RuleDesk.ListBean> desksList;
    private Map<String, Object> map;
    private ArrayList<String> applyBusinessList;
    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_edit_task_list;
    }

    @Override
    protected void init(View view) {
        this.lvlist = (ListView) view.findViewById(R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            SerializableMap myMap = (SerializableMap) bundle.getSerializable("map");
            map = myMap.getMap();
            Map<String, Object> baseQueryMap = (Map<String, Object>) map.get("baseQuery");
            applyBusinessList = (ArrayList<String>) map.get("applyBusinessList");
            pos = bundle.getInt("pos");
            num = bundle.getInt("num");
            isFunction = bundle.getBoolean("isFunction");
            baseQueryMap.put("page", pos);
            baseQueryMap.put("size", num);
            httpPost(ResourceConstant.SEARCH_TASK_ALLOCATION_RULE_DESKTOP, map, SEARCH_TASK_ALLOCATION_RULE_DESKTOP, false, null);//规则桌面
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

//    @Override
//    public void setTitle() {
//        if (peekFragment() instanceof EditTaskRuleListFragment)
//            mActivity.setTitle(getString(R.string.edit_task_rule));
//    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case SEARCH_TASK_ALLOCATION_RULE_DESKTOP:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    RuleDesk desks = JSON.parseObject(commonClass.getData().toString(), RuleDesk.class);
                    desksList = desks.getList();
                    mAdapter = new RuleListAdaper();
                    lvlist.setAdapter(mAdapter);
                }

                break;

        }
    }

    @Override
    public void onReLoad(int code) {
        httpPost(ResourceConstant.SEARCH_TASK_ALLOCATION_RULE_DESKTOP, map, SEARCH_TASK_ALLOCATION_RULE_DESKTOP, false, null);//规则桌面
    }

    private class RuleListAdaper extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.edit_rule_list_item, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        int mItemHeight = lvlist.getHeight() / 8;
                        LogUtils.showInfoLog("lvlist.getHeight():" + lvlist.getHeight());
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        layoutParams.height = mItemHeight;
                        finalConvertView.setLayoutParams(layoutParams);
                    }
                });

                viewHodler.mFullName = (TextView) convertView.findViewById(R.id.full_name);
                viewHodler.mRuleName = (TextView) convertView.findViewById(R.id.rule_name);
                viewHodler.mTime = (TextView) convertView.findViewById(R.id.rule_time);
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            viewHodler.mRuleName.setText(desksList.get(position).getRuleName());
            viewHodler.mFullName.setText(desksList.get(position).getModifierName());
            viewHodler.mTime.setText(desksList.get(position).getUpdateTime().substring(0,10));

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到规则编辑界面
                    EventBusUtil.sendEvent(new Event(HideTitleBean.CODE.A, new HideTitleBean()));
                    ((BaseCardDeskActivity) mActivity).hideSearchInput();
                    ((BaseCardDeskActivity) mActivity).hideSearchTitle();

                    if (isFunction){
                        EditSetRulesFragment fragment = new EditSetRulesFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("taskAllocationUkid", desksList.get(position).getTaskAllocationUkid());
                        bundle.putString("businessId", desksList.get(position).getBelongBusinessUkid());
                        bundle.putStringArrayList("applyBusinessId", applyBusinessList);
                        bundle.putBoolean("isFunction", isFunction);
                        fragment.setArguments(bundle);
                        pushFragment(fragment);
                    }else {
                        EditInformationSetRulesFragment fragment = new EditInformationSetRulesFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("taskAllocationUkid", desksList.get(position).getTaskAllocationUkid());
                        bundle.putString("businessId", desksList.get(position).getBelongBusinessUkid());
                        bundle.putStringArrayList("applyBusinessId", applyBusinessList);
                        bundle.putBoolean("isFunction", isFunction);
                        fragment.setArguments(bundle);
                        pushFragment(fragment);
                    }

                }
            });

            return convertView;
        }

        @Override
        public int getCount() {
            return desksList.size();
        }

        @Override
        public Object getItem(int position) {
            return desksList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class ViewHodler {
        TextView mRuleName;
        TextView mFullName;
        TextView mTime;
    }
}
